package com.ztt.service.impl;

import com.ztt.pojo.entity.zttPurchaseDetail;
import com.ztt.mapper.PurchaseDetailMapper;
import com.ztt.service.PurchaseDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PurchaseDetailServiceImpl implements PurchaseDetailService {

    @Autowired
    PurchaseDetailMapper purchaseDetailMapper;

    @Override
    public List<zttPurchaseDetail> selectAllPurchaseDetail(){
        return purchaseDetailMapper.selectAllPurchaseDetail();
    };

    @Override
    public void insertPurchaseDetail(zttPurchaseDetail purchaseDetail){
        purchaseDetailMapper.insertPurchaseDetail(purchaseDetail);
    };

    @Override
    public zttPurchaseDetail getPurchaseDetailById(zttPurchaseDetail purchaseDetail) {
        return purchaseDetailMapper.getPurchaseDetailById(purchaseDetail);
    }

    @Override
    public List<zttPurchaseDetail> getPurchaseDetailByProductId(zttPurchaseDetail purchaseDetail) {
        return purchaseDetailMapper.getPurchaseDetailByProductId(purchaseDetail);
    }

    @Override
    public List<zttPurchaseDetail> getPurchaseDetailByPurchaseId(zttPurchaseDetail purchaseDetail) {
        return purchaseDetailMapper.getPurchaseDetailByPurchaseId(purchaseDetail);
    }

    @Override
    public void updatePurchaseDetail(zttPurchaseDetail purchaseDetail) {
        int purchaseDetailID = purchaseDetail.getPurchaseDetailID();
        zttPurchaseDetail purchaseDetailDB = purchaseDetailMapper.getPurchaseDetailById(purchaseDetail);
        int purchaseQuantity = purchaseDetail.getPurchaseQuantity();
        if(purchaseQuantity != purchaseDetailDB.getPurchaseQuantity()){
            purchaseDetailDB.setPurchaseQuantity(purchaseQuantity);
        }
        float productPrice = purchaseDetail.getProductPrice();
        if(productPrice != purchaseDetailDB.getProductPrice()){
            purchaseDetailDB.setProductPrice(productPrice);
        }
        float productTotalPrice = purchaseDetailDB.getProductTotalPrice();
        if(productTotalPrice != purchaseDetailDB.getProductTotalPrice()){
            purchaseDetailDB.setProductTotalPrice(productTotalPrice);
        }
        String notes = purchaseDetailDB.getNotes();
        if(!notes.equals("")){
            purchaseDetailDB.setNotes(notes);
        }
        purchaseDetailMapper.updatePurchaseDetail(purchaseDetailDB);
    }

    @Override
    public void deletePurchaseDetail(zttPurchaseDetail purchaseDetail) {

        purchaseDetailMapper.deletePurchaseDetail(purchaseDetail);
    }


}
